
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.ComponentModel;


namespace Swaf.BizObj
{
	[Serializable]
	class BizObjPropertyDescriptor : PropertyDescriptor
	{
		protected BizObjField m_field;
		protected IBizObj m_bizObj;
		protected IBizObjDefinition m_bizObjType;
		protected string m_fieldName;

		public BizObjPropertyDescriptor(BizObjField field, IBizObj bizObj)
			: base(field.Name, new Attribute[0])
		{
			m_field = field;
			m_bizObj = bizObj;
			m_fieldName = field.Name;
		}

		public BizObjPropertyDescriptor(BizObjField field, IBizObjDefinition bizObjType)
			: base(field.Name, new Attribute[0])
		{
			m_field = field;
			m_bizObjType = bizObjType;
			m_fieldName = field.Name;
		}

		public BizObjPropertyDescriptor(BizObjField field, IBizObjDefinition bizObjType, string name)
			: base(name, new Attribute[0])
		{
			m_field = field;
			m_bizObjType = bizObjType;
			m_fieldName = name;
		}

		public string BizObjTypeName
		{
			get
			{
				if (m_bizObj != null)
					return m_bizObj.type.Name;
				else if (m_bizObjType != null)
					return m_bizObjType.Name;
				else
					return null;
			}
		}

		public BizObjField Field
		{
			get
			{
				return m_field;
			}
		}

		public override Type ComponentType
		{
			get { return m_bizObj.GetType(); }
		}

		public override bool IsReadOnly
		{
			get
			{
				//TODO: Check the type of field.  If method, this should return true.
				return false;
			}
		}

		public override Type PropertyType
		{
			get
			{
				if (m_field.SampleValue != null)
					return m_field.SampleValue.GetType();
				else
				{
					string actualFieldType = m_field.FactoryType;
					IClassFactory factory = Application.currentApp.factories["BizObjBizFieldTypes"];
					object initValue = factory.createFromPattern(actualFieldType);
					if (initValue != null)
						return initValue.GetType();
					else
						return null;
				}
			}
		}

		public override bool CanResetValue(object component)
		{
			return false;
		}

		public override object GetValue(object component)
		{
			if (component is IBizObj)
				return ((IBizObj)component).get(m_fieldName);
			return null;
		}

		public override void ResetValue(object component)
		{
		}

		public override void SetValue(object component, object value)
		{
			if (component is IBizObj)
				((IBizObj)component).put(m_fieldName, value);
		}

		public override bool ShouldSerializeValue(object component)
		{
			return false;
		}
	}
}
